package com.yf.biodemo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;

/**
 * BIO 同步阻塞IO  服务端demo
 * @author yf
 */
public class BIOServer {
    public static void main(String[] args) throws IOException {
        int port = 12000;
        ServerSocket serverSocket = null;
        //创建绑定到特定端口的服务器套接字
        serverSocket = new ServerSocket(port);
        //侦听并接受到此套接字的连接
        Socket socket = serverSocket.accept();
        //获取输入流，解析接收到的消息
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //获取输出流，响应消息给client
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        while (true) {
            String body = in.readLine();
            if (body == null) {
                break;
            }
            System.out.println("接收到client信息：" + body);
            //响应client
            out.println("已接收到客户端消息["+body+"] "+ LocalDateTime.now());
        }
        in.close();
        out.close();
        socket.close();
    }
}
